<template>
  <el-container class="home-container">
    <!-- 头部导航 -->
    <el-header class="el-header">
      <div>
        <img src="..\assets\img\三衍.png" class="img01" alt />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- 宽度切换 -->
      <el-aside :width="siCollapse ? '64px' : '200px'">
        <!-- 图标切换 -->
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="siCollapse ? 'el-icon-circle-plus-outline' : 'el-icon-remove-outline'"></i>
        </div>
        <!-- 侧边栏菜单 -->
        <!-- 
              background-color="#323744"     背景色
              text-color="#fff"              字体色
              active-text-color="#23AAF2"    选中高亮
              :unique-opened="true"          只开启一个菜单
              :collapse="siCollapse"         菜单栏缩小
              :collapse-transition="false"   关闭菜单栏缩小动画
              :router="true" 开启路由模式
              
        -->
        <el-menu
          background-color="#323744"
          text-color="#fff"
          active-text-color="#23AAF2"
          :unique-opened="true"
          :collapse="siCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一級菜單 -->
          <el-submenu :index="menu.mid+''" v-for="menu in menus" :key="menu.mid">
            <!-- 一級菜單的模板 -->
            <template slot="title">
              <!-- 圖標 -->
              <i :class="menu.icon"></i>
              <!-- 文本 -->
              <span>{{menu.mname}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="menuc.path"
              v-for="menuc in menu.menus"
              :key="menuc.mid"
              @click="saveLinkStatus(menuc.path)"
            >
              <!-- 圖標 -->
              <i :class="menuc.icon"></i>
              <!-- 文本 -->
              <span>{{menuc.mname}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menus: [],
      siCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenus() {
      const { data: res } = await this.$http.get('user/menus')
      if (!res.flag) return this.$message.error(res.message)
      this.menus = res.data
    },
    // 点击折叠展开
    toggleCollapse() {
      this.siCollapse = !this.siCollapse
    },
    // 保存连接状态 saveLinkStatus
    saveLinkStatus(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .img01 {
    width: 100px;
    height: 40px;
    opacity: 0.8;
    border-radius: 40% 0 40% 0;
  }
  .img01:hover {
    opacity: 1; //0-1 透明
  }
}

.el-header {
  background-color: #373c41;
  display: flex;
  justify-content: space-between; //两端对齐
  padding-left: 0;
  align-items: center; //  flex元素居中
  color: #fff;

  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #323744;
}

.el-main {
  background-color: #eaedf2;
}
.el-menu {
  border-right: 0;
}
.toggle-button {
  background-color: #485164;
  height: 24px;
  line-height: 24px;
  font-size: 10px;
  color: #fff;
  text-align: center; // 文本居中
  cursor: pointer; // 小手
}
</style>